﻿//-----------------------------------------------------------------------
// <copyright file="HomeController.cs" company="SampleThreeTier">
//     Sample 3 tier application by Anurag.
// </copyright>
//-----------------------------------------------------------------------
namespace PresentationLayer.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using AutoMapper;
    using BusinessEntity;
    using PresentationLayer.Models;

    /// <summary>
    /// The home controller
    /// </summary>
    public class HomeController : Controller
    {
        /// <summary>
        /// The index action method.
        /// </summary>
        /// <returns>The Action result</returns>
        [HttpGet]
        public ActionResult Index()
        {
            var bookProcessingLogics = BusinessFactory.BookProcessingFactory.GetBookProcessingLogicsInstance();
            var bookEntities = bookProcessingLogics.GetBookDetails();
            var bookModels = Mapper.Map<List<BookModel>>(bookEntities);
            return this.View(bookModels);
        }

        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <returns>The Action Result</returns>
        [HttpGet]
        public ActionResult Create()
        {
            var bookModel = new BookModel();
            return this.View("Create", bookModel);
        }

        /// <summary>
        /// Submits the book details.
        /// </summary>
        /// <param name="bookModel">The book model.</param>
        /// <returns>The Action result</returns>
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult SubmitBookDetails(BookModel bookModel)
        {
            if (null != bookModel && ModelState.IsValid)
            {
                var bookProcessingLogics = BusinessFactory.BookProcessingFactory.GetBookProcessingLogicsInstance();
                var bookEntity = Mapper.Map<BookEntity>(bookModel);
                var result = bookProcessingLogics.AddBookDetails(bookEntity);
                return this.View("SubmitSuccess", result);
            }

            return this.View("Error");
        }

        /// <summary>
        /// Edits the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>The action result</returns>
        [HttpGet]
        public ActionResult Edit(int id)
        {
            var bookProcessingLogics = BusinessFactory.BookProcessingFactory.GetBookProcessingLogicsInstance();
            var bookEntity = bookProcessingLogics.GetParticularBookDetails(id);
            if (null != bookEntity)
            {
                var bookModel = Mapper.Map<BookModel>(bookEntity);
                return this.View("Edit", bookModel);
            }

            return this.View("Error");
        }

        /// <summary>
        /// Edits the specified book model.
        /// </summary>
        /// <param name="bookModel">The book model.</param>
        /// <returns>The action result</returns>
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Edit(BookModel bookModel)
        {
            if (null != bookModel && ModelState.IsValid)
            {
                var bookProcessingLogics = BusinessFactory.BookProcessingFactory.GetBookProcessingLogicsInstance();
                var bookEntity = Mapper.Map<BookEntity>(bookModel);
                var result = bookProcessingLogics.UpdateBookDetails(bookEntity);
                return this.View("EditSuccess", result);
            }

            return this.View("Error");
        }

        /// <summary>
        /// Deletes the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>The action result</returns>
        [HttpGet]
        public ActionResult Delete(int id)
        {
            if (id >= 0)
            {
                var bookProcessingLogics = BusinessFactory.BookProcessingFactory.GetBookProcessingLogicsInstance();
                var result = bookProcessingLogics.DeleteBookDetails(id);
                if (result)
                {
                    return this.View("DeleteSuccess");
                }
            }

            return this.View("Error");
        }
    }
}
